package com.lsw.util;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * 屏幕分辨率和像素转换
 *
 * @author heiyue heiyue623@126.com
 * @ClassName: DimenUtils
 * @Description:
 * @date 2014-6-6 上午11:09:19
 */
public class DimenUtils {

    /**
     * O
     * 获取屏幕的尺寸
     *
     * @param context
     * @return int[0]=screenWidth int[1]=screenHeight
     */
    public static int[] getScreenSize(Activity context) {
        // 获取屏幕密度（方法3）
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);

        float density = dm.density;      // 屏幕密度（像素比例：0.75/1.0/1.5/2.0）
        int screenWidth = (int) (dm.widthPixels * density + 0.5f);      // 屏幕宽（px，如：480px）
        int screenHeight = (int) (dm.heightPixels * density + 0.5f);     // 屏幕高（px，如：800px）
        return new int[]{dm.widthPixels, dm.heightPixels};
    }

    public static int sp2px(Context context, float spValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int dip2px(Context context, int dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static float getTextSize(Activity context, int rate) {
        float size = 0;
        WindowManager wm = context.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        size = (float) width / 1080;
        return size * rate;
    }

}